home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / djgpp / src / gas-211 / gas / write.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-30  |  46.3 KB  |  1,704 lines

  1. /* write.c - emit .o file
  2.    Copyright (C) 1986, 1987, 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
  3.  
  4.    This file is part of GAS, the GNU Assembler.
  5.  
  6.    GAS is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 2, or (at your option)
  9.    any later version.
  10.  
  11.    GAS is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with GAS; see the file COPYING.  If not, write to
  18.    the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* This thing should be set up to do byteordering correctly.  But... */
  21.  
  22. #include "as.h"
  23. #include "subsegs.h"
  24. #include "obstack.h"
  25. #include "output-file.h"
  26.  
  27. /* The NOP_OPCODE is for the alignment fill value.  Fill it with a nop
  28.    instruction so that the disassembler does not choke on it.  */
  29. #ifndef NOP_OPCODE
  30. #define NOP_OPCODE 0x00
  31. #endif
  32.  
  33. #ifndef WORKING_DOT_WORD
  34. extern CONST int md_short_jump_size;
  35. extern CONST int md_long_jump_size;
  36. #endif
  37.  
  38. #ifndef BFD_ASSEMBLER
  39.  
  40. #ifndef MANY_SEGMENTS
  41. struct frag *text_frag_root;
  42. struct frag *data_frag_root;
  43. struct frag *bss_frag_root;
  44.  
  45. struct frag *text_last_frag;    /* Last frag in segment. */
  46. struct frag *data_last_frag;    /* Last frag in segment. */
  47. static struct frag *bss_last_frag;    /* Last frag in segment. */
  48. #endif
  49.  
  50. static object_headers headers;
  51. long string_byte_count;
  52. static char *the_object_file;
  53. char *next_object_file_charP;    /* Tracks object file bytes. */
  54.  
  55. #ifndef OBJ_VMS
  56. int magic_number_for_object_file = DEFAULT_MAGIC_NUMBER_FOR_OBJECT_FILE;
  57. #endif
  58.  
  59. #endif /* BFD_ASSEMBLER */
  60.  
  61. static long fixup_segment PARAMS ((fixS * fixP, segT this_segment_type));
  62. static relax_addressT relax_align PARAMS ((relax_addressT addr, long align));
  63. void relax_segment PARAMS ((struct frag * seg_frag_root, segT seg_type));
  64.  
  65. /*
  66.  *            fix_new()
  67.  *
  68.  * Create a fixS in obstack 'notes'.
  69.  */
  70. fixS *
  71. fix_new (frag, where, size, add_symbol, sub_symbol, offset, pcrel, r_type)
  72.      fragS *frag;        /* Which frag? */
  73.      int where;            /* Where in that frag? */
  74.      short int size;        /* 1, 2, or 4 usually. */
  75.      symbolS *add_symbol;    /* X_add_symbol. */
  76.      symbolS *sub_symbol;    /* X_subtract_symbol. */
  77.      long offset;        /* X_add_number. */
  78.      int pcrel;            /* TRUE if PC-relative relocation. */
  79. #ifdef BFD_ASSEMBLER
  80.      bfd_reloc_code_real_type r_type; /* Relocation type */
  81. #else
  82.      int r_type;        /* Relocation type */
  83. #endif
  84. {
  85.   fixS *fixP;
  86.  
  87.   fixP = (fixS *) obstack_alloc (¬es, sizeof (fixS));
  88.  
  89.   fixP->fx_frag = frag;
  90.   fixP->fx_where = where;
  91.   fixP->fx_size = size;
  92.   fixP->fx_addsy = add_symbol;
  93.   fixP->fx_subsy = sub_symbol;
  94.   fixP->fx_offset = offset;
  95.   fixP->fx_pcrel = pcrel;
  96. #if defined(NEED_FX_R_TYPE) || defined (BFD_ASSEMBLER)
  97.   fixP->fx_r_type = r_type;
  98. #endif
  99.   fixP->fx_im_disp = 0;
  100.   fixP->fx_pcrel_adjust = 0;
  101.   fixP->fx_bit_fixP = 0;
  102.   fixP->fx_addnumber = 0;
  103.  
  104. #ifdef TC_something
  105.   fixP->fx_bsr = 0;
  106. #endif
  107. #ifdef TC_I960
  108.   fixP->fx_callj = 0;
  109. #endif
  110.  
  111.   /* Usually, we want relocs sorted numerically, but while
  112.      comparing to older versions of gas that have relocs
  113.      reverse sorted, it is convenient to have this compile
  114.      time option.  xoxorich. */
  115.  
  116.   {
  117.  
  118. #ifdef BFD_ASSEMBLER
  119.     fixS **seg_fix_rootP = & (seg_info (now_seg)->fix_root);
  120.     fixS **seg_fix_tailP = & (seg_info (now_seg)->fix_tail);
  121. #endif
  122.  
  123. #ifdef REVERSE_SORT_RELOCS
  124.  
  125.     fixP->fx_next = *seg_fix_rootP;
  126.     *seg_fix_rootP = fixP;
  127.  
  128. #else /* REVERSE_SORT_RELOCS */
  129.  
  130.     fixP->fx_next = NULL;
  131.  
  132.     if (*seg_fix_tailP)
  133.       (*seg_fix_tailP)->fx_next = fixP;
  134.     else
  135.       *seg_fix_rootP = fixP;
  136.     *seg_fix_tailP = fixP;
  137.  
  138. #endif /* REVERSE_SORT_RELOCS */
  139.  
  140.   }
  141.  
  142.   return fixP;
  143. }
  144.  
  145. /* Append a string onto another string, bumping the pointer along.  */
  146. void
  147. append (charPP, fromP, length)
  148.      char **charPP;
  149.      char *fromP;
  150.      unsigned long length;
  151. {
  152.   /* Don't trust memcpy() of 0 chars. */
  153.   if (length == 0)
  154.     return;
  155.  
  156.   memcpy (*charPP, fromP, (int) length);
  157.   *charPP += length;
  158. }
  159.  
  160. #ifndef BFD_ASSEMBLER 
  161. int section_alignment[SEG_MAXIMUM_ORDINAL];
  162. #endif
  163.  
  164. /*
  165.  * This routine records the largest alignment seen for each segment.
  166.  * If the beginning of the segment is aligned on the worst-case
  167.  * boundary, all of the other alignments within it will work.  At
  168.  * least one object format really uses this info.
  169.  */
  170. void 
  171. record_alignment (seg, align)
  172.      /* Segment to which alignment pertains */
  173.      segT seg;
  174.      /* Alignment, as a power of 2 (e.g., 1 => 2-byte boundary, 2 => 4-byte
  175.     boundary, etc.)  */
  176.      int align;
  177. {
  178. #ifdef BFD_ASSEMBLER
  179.   if (align > bfd_get_section_alignment (stdoutput, seg))
  180.     bfd_set_section_alignment (stdoutput, seg, align);
  181. #else
  182.   if (align > section_alignment[(int) seg])
  183.     section_alignment[(int) seg] = align;
  184. #endif
  185. }
  186.  
  187. #if defined (BFD_ASSEMBLER) || ! defined (BFD)
  188.  
  189. static fragS *
  190. chain_frchains_together_1 (section, frchp)
  191.      segT section;
  192.      struct frchain *frchp;
  193. {
  194.   fragS dummy, *prev_frag = &dummy;
  195.   for (; frchp && frchp->frch_seg == section; frchp = frchp->frch_next)
  196.     {
  197.       prev_frag->fr_next = frchp->frch_root;
  198.       prev_frag = frchp->frch_last;
  199.     }
  200.   prev_frag->fr_next = 0;
  201.   return prev_frag;
  202. }
  203.  
  204. #endif
  205.  
  206. #ifdef BFD_ASSEMBLER
  207.  
  208. static void
  209. chain_frchains_together (abfd, section, xxx)
  210.      bfd *abfd;            /* unused */
  211.      segT section;
  212.      char *xxx;            /* unused */
  213. {
  214.   segment_info_type *info;
  215.  
  216.   /* BFD may have introduced its own sections without using
  217.      subseg_new, so it is possible that seg_info is NULL.  */
  218.   info = seg_info (section);
  219.   if (info != (segment_info_type *) NULL)
  220.     chain_frchains_together_1 (section, info->frchainP);
  221. }
  222.  
  223. #endif
  224.  
  225. #ifndef BFD
  226.  
  227. void 
  228. remove_subsegs (head, seg, root, last)
  229.      frchainS *head;
  230.      int seg;
  231.      fragS **root;
  232.      fragS **last;
  233. {
  234.   *root = head->frch_root;
  235.   *last = chain_frchains_together_1 (seg, head);
  236. }
  237.  
  238. #endif /* BFD */
  239.  
  240. static void
  241. cvt_frag_to_fill (x, fragP)
  242. #ifdef BFD_ASSEMBLER
  243.      segT x;
  244. #else
  245.      object_headers *x;
  246. #endif
  247.      fragS *fragP;
  248. {
  249. #ifdef BFD_ASSEMBLER
  250.   segT sec = x;
  251. #else
  252.   object_headers *headers = x;
  253. #endif
  254.  
  255.   switch (fragP->fr_type)
  256.     {
  257.     case rs_align:
  258.     case rs_org:
  259. #ifdef HANDLE_ALIGN
  260.       HANDLE_ALIGN (fragP);
  261. #endif
  262.       fragP->fr_type = rs_fill;
  263.       know (fragP->fr_var == 1);
  264.       know (fragP->fr_next != NULL);
  265.  
  266.       fragP->fr_offset = (fragP->fr_next->fr_address
  267.               - fragP->fr_address
  268.               - fragP->fr_fix);
  269.       break;
  270.  
  271.     case rs_fill:
  272.       break;
  273.  
  274.     case rs_machine_dependent:
  275. #ifdef BFD_ASSEMBLER
  276.       md_convert_frag (stdoutput, sec, fragP);
  277. #else
  278.       md_convert_frag (headers, fragP);
  279. #endif
  280.  
  281.       assert (fragP->fr_next == NULL || (fragP->fr_next->fr_address - fragP->fr_address == fragP->fr_fix));
  282.  
  283.       /*
  284.        * After md_convert_frag, we make the frag into a ".space 0".
  285.        * Md_convert_frag() should set up any fixSs and constants
  286.        * required.
  287.        */
  288.       frag_wane (fragP);
  289.       break;
  290.  
  291. #ifndef WORKING_DOT_WORD
  292.     case rs_broken_word:
  293.       {
  294.     struct broken_word *lie;
  295.  
  296.     if (fragP->fr_subtype)
  297.       {
  298.         fragP->fr_fix += md_short_jump_size;
  299.         for (lie = (struct broken_word *) (fragP->fr_symbol);
  300.          lie && lie->dispfrag == fragP;
  301.          lie = lie->next_broken_word)
  302.           if (lie->added == 1)
  303.         fragP->fr_fix += md_long_jump_size;
  304.       }
  305.     frag_wane (fragP);
  306.       }
  307.       break;
  308. #endif
  309.  
  310.     default:
  311.       BAD_CASE (fragP->fr_type);
  312.       break;
  313.     }
  314. }
  315.  
  316. #ifdef BFD_ASSEMBLER
  317. static void
  318. relax_and_size_seg (abfd, sec, xxx)
  319.      bfd *abfd;
  320.      asection *sec;
  321.      char *xxx;
  322. {
  323.   flagword flags;
  324.  
  325.   flags = bfd_get_section_flags (abfd, sec);
  326.  
  327.   if (flags & SEC_ALLOC)
  328.     {
  329.       fragS *fragp;
  330.       segment_info_type *seginfo;
  331.       int x;
  332.       unsigned long size, newsize;
  333.  
  334.       seginfo = (segment_info_type *) bfd_get_section_userdata (abfd, sec);
  335.       if (seginfo->frchainP)
  336.     {
  337.       relax_segment (seginfo->frchainP->frch_root, sec);
  338.       for (fragp = seginfo->frchainP->frch_root; fragp; fragp = fragp->fr_next)
  339.         cvt_frag_to_fill (sec, fragp);
  340.       for (fragp = seginfo->frchainP->frch_root;
  341.            fragp->fr_next;
  342.            fragp = fragp->fr_next)
  343.         /* walk to last elt */;
  344.       size = fragp->fr_address;
  345.     }
  346.       else
  347.     size = 0;
  348.       if (size > 0)
  349.     {
  350.       flags |= SEC_HAS_CONTENTS;
  351.       /* @@ This is just an approximation.  */
  352.       if (seginfo->fix_root)
  353.         flags |= SEC_RELOC;
  354.       x = bfd_set_section_flags (abfd, sec, flags);
  355.       assert (x == true);
  356.     }
  357.       size = md_section_align (sec, size);
  358.       x = bfd_set_section_size (abfd, sec, size);
  359.       assert (x == true);
  360.  
  361.       /* If the size had to be rounded up, add some padding in the last
  362.      non-empty frag.  */
  363.       newsize = bfd_get_section_size_before_reloc (sec);
  364.       assert (newsize >= size);
  365.       if (size != newsize)
  366.     {
  367.       fragS *last = seginfo->frchainP->frch_last;
  368.       fragp = seginfo->frchainP->frch_root;
  369.       while (fragp->fr_next != last)
  370.         fragp = fragp->fr_next;
  371.       last->fr_address = size;
  372.       fragp->fr_offset += newsize - size;
  373.     }
  374.     }
  375. #ifdef tc_frob_section
  376.   tc_frob_section (sec);
  377. #endif
  378. #ifdef obj_frob_section
  379.   obj_frob_section (sec);
  380. #endif
  381. }
  382.  
  383. static void
  384. write_contents (abfd, sec, xxx)
  385.      bfd *abfd;
  386.      asection *sec;
  387.      char *xxx;
  388. {
  389.   segment_info_type *seginfo = seg_info (sec);
  390.   unsigned long offset = 0;
  391.   fragS *frags;
  392.   int i, n;
  393.   arelent **relocs;
  394.   fixS *fixp;
  395.  
  396.   if (! (bfd_get_section_flags (abfd, sec) & SEC_LOAD))
  397.     return;
  398.  
  399.   fixup_segment (seginfo->fix_root, sec);
  400.  
  401.   n = 0;
  402.   for (i = 0, fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
  403.     {
  404.       n++;
  405.       if (fixp->fx_addsy)
  406.     {
  407.       symbolS *sym = fixp->fx_addsy;
  408.       asection *sec = sym->bsym->section;
  409.       if (sec == &bfd_und_section
  410.           || sec == &bfd_abs_section
  411.           || sec == &bfd_com_section)
  412.         continue;
  413.       if (sym->bsym == sec->symbol)
  414.         continue;
  415.       /* If the section symbol isn't going to be output, the relocs
  416.          at least should still work.  If not, figure out what to do
  417.          when we run into that case.  */
  418.       fixp->fx_offset += S_GET_VALUE (sym);
  419.       fixp->fx_addsy = symbol_find (sec->name);
  420.       if (!fixp->fx_addsy)
  421.         {
  422.           fixp->fx_addsy = symbol_make (sec->name);
  423.           fixp->fx_addsy->bsym = sec->symbol;
  424.         }
  425.     }
  426.     }
  427.  
  428.   /* Force calculations (size, vma) to get done.  */
  429.   bfd_set_section_contents (stdoutput, sec, "", 0, 0);
  430.  
  431.   /* Set up reloc information as well.  */
  432.   relocs = (arelent **) bfd_alloc_by_size_t (stdoutput,
  433.                          n * sizeof (arelent *));
  434.  
  435.   i = 0;
  436.   for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
  437.     {
  438.       arelent *reloc;
  439.       extern arelent *tc_gen_reloc ();
  440.       char *data;
  441.       bfd_reloc_status_type s;
  442.  
  443.       if (fixp->fx_addsy == 0)
  444.     {
  445.       /* @@ Need some other flag to indicate which have already
  446.          been performed...  */
  447.       n--;
  448.       continue;
  449.     }
  450.       reloc = tc_gen_reloc (sec, fixp);
  451.       if (!reloc)
  452.     {
  453.       n--;
  454.       continue;
  455.     }
  456.       data = fixp->fx_frag->fr_literal + fixp->fx_where;
  457.       if (fixp->fx_where + 4
  458.       > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset)
  459.     abort ();
  460.       s = bfd_perform_relocation (stdoutput, reloc, data - reloc->address,
  461.                   sec, stdoutput);
  462.       switch (s)
  463.     {
  464.     case bfd_reloc_ok:
  465.       break;
  466.     default:
  467.       as_fatal ("bad return from bfd_perform_relocation");
  468.     }
  469.       relocs[i++] = reloc;
  470.     }
  471.       
  472.   if (n)
  473.     bfd_set_reloc (stdoutput, sec, relocs, n);
  474.  
  475.   /* Write out the frags.  */
  476.   for (frags = seginfo->frchainP->frch_root;
  477.        frags;
  478.        frags = frags->fr_next)
  479.     {
  480.       int x;
  481.       unsigned long fill_size;
  482.       char *fill_literal;
  483.       long count;
  484.  
  485.       assert (frags->fr_type == rs_fill);
  486.       if (frags->fr_fix)
  487.     {
  488.       x = bfd_set_section_contents (stdoutput, sec,
  489.                     frags->fr_literal, offset,
  490.                     frags->fr_fix);
  491.       assert (x == true);
  492.       offset += frags->fr_fix;
  493.     }
  494.       fill_literal = frags->fr_literal + frags->fr_fix;
  495.       fill_size = frags->fr_var;
  496.       count = frags->fr_offset;
  497.       assert (count >= 0);
  498.       if (fill_size && count)
  499.     while (count--)
  500.       {
  501.         x = bfd_set_section_contents (stdoutput, sec,
  502.                       fill_literal, offset, fill_size);
  503.         assert (x == true);
  504.         offset += fill_size;
  505.       }
  506.     }
  507. }
  508. #endif
  509.  
  510. #if defined (BFD_ASSEMBLER) || !defined (BFD)
  511.  
  512. void 
  513. write_object_file ()
  514. {
  515.   register struct frchain *frchainP;    /* Track along all frchains. */
  516.   register fragS *fragP;    /* Track along all frags. */
  517.   register struct frchain *next_frchainP;
  518.   register fragS **prev_fragPP;
  519.  
  520.   long object_file_size;
  521.  
  522.   /* Do we really want to write it?  */
  523.   {
  524.     int n_warns, n_errs;
  525.     n_warns = had_warnings ();
  526.     n_errs = had_errors ();
  527.     /* The -Z flag indicates that an object file should be generated,
  528.        regardless of warnings and errors.  */
  529.     if (flagseen['Z'])
  530.       {
  531.     if (n_warns || n_errs)
  532.       as_warn ("%d error%s, %d warning%s, generating bad object file.\n",
  533.            n_errs, n_errs == 1 ? "" : "s",
  534.            n_warns, n_warns == 1 ? "" : "s");
  535.       }
  536.     else
  537.       {
  538.     if (n_errs)
  539.       as_fatal ("%d error%s, %d warning%s, no object file generated.\n",
  540.             n_errs, n_errs == 1 ? "" : "s",
  541.             n_warns, n_warns == 1 ? "" : "s");
  542.       }
  543.   }
  544.  
  545. #ifdef    OBJ_VMS
  546.   /*
  547.    *    Under VMS we try to be compatible with VAX-11 "C".  Thus, we
  548.    *    call a routine to check for the definition of the procedure
  549.    *    "_main", and if so -- fix it up so that it can be program
  550.    *    entry point.
  551.    */
  552.   VMS_Check_For_Main ();
  553. #endif /* VMS */
  554.  
  555.   /* After every sub-segment, we fake an ".align ...". This conforms to
  556.      BSD4.2 brane-damage. We then fake ".fill 0" because that is the kind of
  557.      frag that requires least thought. ".align" frags like to have a
  558.      following frag since that makes calculating their intended length
  559.      trivial.
  560.  
  561.      @@ Is this really necessary??  */
  562. #ifndef SUB_SEGMENT_ALIGN
  563. #ifdef BFD_ASSEMBLER
  564. #define SUB_SEGMENT_ALIGN(SEG) (0)
  565. #else
  566. #define SUB_SEGMENT_ALIGN(SEG) (2)
  567. #endif
  568. #endif
  569.   for (frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next)
  570.     {
  571. #ifdef BFD_ASSEMBLER
  572.       subseg_set (frchainP->frch_seg, frchainP->frch_subseg);
  573. #else
  574.       subseg_new (frchainP->frch_seg, frchainP->frch_subseg);
  575. #endif
  576.       frag_align (SUB_SEGMENT_ALIGN (now_seg), NOP_OPCODE);
  577.       /* frag_align will have left a new frag.
  578.      Use this last frag for an empty ".fill".
  579.  
  580.      For this segment ...
  581.      Create a last frag. Do not leave a "being filled in frag".  */
  582.       frag_wane (frag_now);
  583.       frag_now->fr_fix = 0;
  584.       know (frag_now->fr_next == NULL);
  585.       /* know( frags . obstack_c_base == frags . obstack_c_next_free ); */
  586.       /* Above shows we haven't left a half-completed object on obstack. */
  587.     }
  588.  
  589.   /* From now on, we don't care about sub-segments.  Build one frag chain
  590.      for each segment. Linked thru fr_next.  */
  591.  
  592. #ifdef BFD_ASSEMBLER
  593.   /* Remove the sections created by gas for its own purposes.  */
  594.   {
  595.     asection **seclist, *sec;
  596.     seclist = &stdoutput->sections;
  597.     while (seclist && *seclist)
  598.       {
  599.     sec = *seclist;
  600.     while (sec == big_section
  601.            || sec == reg_section
  602.            || sec == pass1_section
  603.            || sec == diff_section
  604.            || sec == absent_section)
  605.       {
  606.         sec = sec->next;
  607.         *seclist = sec;
  608.         stdoutput->section_count--;
  609.         if (!sec)
  610.           break;
  611.       }
  612.     if (*seclist)
  613.       seclist = &(*seclist)->next;
  614.       }
  615.   }
  616.  
  617.   bfd_map_over_sections (stdoutput, chain_frchains_together, (char *) 0);
  618. #else
  619.   remove_subsegs (frchain_root, SEG_TEXT, &text_frag_root, &text_last_frag);
  620.   remove_subsegs (data0_frchainP, SEG_DATA, &data_frag_root, &data_last_frag);
  621.   remove_subsegs (bss0_frchainP, SEG_BSS, &bss_frag_root, &bss_last_frag);
  622. #endif
  623.  
  624.   /* We have two segments. If user gave -R flag, then we must put the
  625.      data frags into the text segment. Do this before relaxing so
  626.      we know to take advantage of -R and make shorter addresses.  */
  627. #if !defined (OBJ_AOUT) || defined (BFD_ASSEMBLER)
  628.   if (flagseen['R'])
  629.     {
  630. #ifdef BFD_ASSEMBLER
  631.       seg_info (text_section)->frchainP->frch_last->fr_next =
  632.     seg_info (data_section)->frchainP->frch_root;
  633.       seg_info (text_section)->frchainP->frch_last =
  634.     seg_info (data_section)->frchainP->frch_last;
  635.       seg_info (data_section)->frchainP = 0;
  636. #else
  637.       fixS *tmp;
  638.  
  639.       text_last_frag->fr_next = data_frag_root;
  640.       text_last_frag = data_last_frag;
  641.       data_last_frag = NULL;
  642.       data_frag_root = NULL;
  643.       if (text_fix_root)
  644.     {
  645.       for (tmp = text_fix_root; tmp->fx_next; tmp = tmp->fx_next);;
  646.       tmp->fx_next = data_fix_root;
  647.       text_fix_tail = data_fix_tail;
  648.     }
  649.       else
  650.     text_fix_root = data_fix_root;
  651.       data_fix_root = NULL;
  652. #endif
  653.     }
  654. #endif
  655.  
  656. #ifdef BFD_ASSEMBLER
  657.   bfd_map_over_sections (stdoutput, relax_and_size_seg, (char *) 0);
  658. #else
  659.   relax_segment (text_frag_root, SEG_TEXT);
  660.   relax_segment (data_frag_root, SEG_DATA);
  661.   relax_segment (bss_frag_root, SEG_BSS);
  662.   /*
  663.    * Now the addresses of frags are correct within the segment.
  664.    */
  665.  
  666.   know (text_last_frag->fr_type == rs_fill && text_last_frag->fr_offset == 0);
  667.   H_SET_TEXT_SIZE (&headers, text_last_frag->fr_address);
  668.   text_last_frag->fr_address = H_GET_TEXT_SIZE (&headers);
  669.  
  670.   /*
  671.    * Join the 2 segments into 1 huge segment.
  672.    * To do this, re-compute every rn_address in the SEG_DATA frags.
  673.    * Then join the data frags after the text frags.
  674.    *
  675.    * Determine a_data [length of data segment].
  676.    */
  677.   if (data_frag_root)
  678.     {
  679.       register relax_addressT slide;
  680.  
  681.       know ((text_last_frag->fr_type == rs_fill) && (text_last_frag->fr_offset == 0));
  682.  
  683.       H_SET_DATA_SIZE (&headers, data_last_frag->fr_address);
  684.       data_last_frag->fr_address = H_GET_DATA_SIZE (&headers);
  685.       slide = H_GET_TEXT_SIZE (&headers);    /* & in file of the data segment. */
  686. #ifdef OBJ_BOUT
  687. #define RoundUp(N,S) (((N)+(S)-1)&-(S))
  688.       /* For b.out: If the data section has a strict alignment
  689.      requirement, its load address in the .o file will be
  690.      rounded up from the size of the text section.  These
  691.      two values are *not* the same!  Similarly for the bss
  692.      section....  */
  693.       slide = RoundUp (slide, 1 << section_alignment[SEG_DATA]);
  694. #endif
  695.  
  696.       for (fragP = data_frag_root; fragP; fragP = fragP->fr_next)
  697.     {
  698.       fragP->fr_address += slide;
  699.     }            /* for each data frag */
  700.  
  701.       know (text_last_frag != 0);
  702.       text_last_frag->fr_next = data_frag_root;
  703.     }
  704.   else
  705.     {
  706.       H_SET_DATA_SIZE (&headers, 0);
  707.     }
  708.  
  709. #ifdef OBJ_BOUT
  710.   /* See above comments on b.out data section address.  */
  711.   {
  712.     long bss_vma;
  713.     if (data_last_frag == 0)
  714.       bss_vma = H_GET_TEXT_SIZE (&headers);
  715.     else
  716.       bss_vma = data_last_frag->fr_address;
  717.     bss_vma = RoundUp (bss_vma, 1 << section_alignment[SEG_BSS]);
  718.     bss_address_frag.fr_address = bss_vma;
  719.   }
  720. #else /* ! OBJ_BOUT */
  721.   bss_address_frag.fr_address = (H_GET_TEXT_SIZE (&headers) +
  722.                  H_GET_DATA_SIZE (&headers));
  723.  
  724.  
  725.   /* Slide all the frags */
  726.   if (bss_frag_root)
  727.     {
  728.       relax_addressT slide = bss_address_frag.fr_address;
  729.  
  730.       for (fragP = bss_frag_root; fragP; fragP = fragP->fr_next)
  731.     {
  732.       fragP->fr_address += slide;
  733.     }            /* for each bss frag */
  734.     }
  735.  
  736. #endif /* ! OBJ_BOUT */
  737.  
  738.   if (bss_last_frag)
  739.     H_SET_BSS_SIZE (&headers,
  740.             bss_last_frag->fr_address - bss_frag_root->fr_address);
  741.   else
  742.     H_SET_BSS_SIZE (&headers, 0);
  743. #endif /* BFD_ASSEMBLER */
  744.  
  745. #ifndef BFD_ASSEMBLER
  746.   /*
  747.    *
  748.    * Crawl the symbol chain.
  749.    *
  750.    * For each symbol whose value depends on a frag, take the address of
  751.    * that frag and subsume it into the value of the symbol.
  752.    * After this, there is just one way to lookup a symbol value.
  753.    * Values are left in their final state for object file emission.
  754.    * We adjust the values of 'L' local symbols, even if we do
  755.    * not intend to emit them to the object file, because their values
  756.    * are needed for fix-ups.
  757.    *
  758.    * Unless we saw a -L flag, remove all symbols that begin with 'L'
  759.    * from the symbol chain.  (They are still pointed to by the fixes.)
  760.    *
  761.    * Count the remaining symbols.
  762.    * Assign a symbol number to each symbol.
  763.    * Count the number of string-table chars we will emit.
  764.    * Put this info into the headers as appropriate.
  765.    *
  766.    */
  767.   know (zero_address_frag.fr_address == 0);
  768.   string_byte_count = sizeof (string_byte_count);
  769.  
  770.   obj_crawl_symbol_chain (&headers);
  771.  
  772.   if (string_byte_count == sizeof (string_byte_count))
  773.     string_byte_count = 0;
  774.  
  775.   H_SET_STRING_SIZE (&headers, string_byte_count);
  776.  
  777.   /*
  778.    * Addresses of frags now reflect addresses we use in the object file.
  779.    * Symbol values are correct.
  780.    * Scan the frags, converting any ".org"s and ".align"s to ".fill"s.
  781.    * Also converting any machine-dependent frags using md_convert_frag();
  782.    */
  783.   subseg_change (SEG_TEXT, 0);
  784.  
  785.   for (fragP = text_frag_root; fragP; fragP = fragP->fr_next)
  786.     {
  787.       cvt_frag_to_fill (&headers, fragP);
  788.  
  789.       /* Some assert macros don't work with # directives mixed in.  */
  790. #ifndef NDEBUG
  791.       if (!(fragP->fr_next == NULL
  792. #ifdef OBJ_BOUT
  793.         || fragP->fr_next == data_frag_root
  794. #endif
  795.         || ((fragP->fr_next->fr_address - fragP->fr_address)
  796.         == (fragP->fr_fix + fragP->fr_offset * fragP->fr_var))))
  797.     abort ();
  798. #endif
  799.     }
  800. #endif /* ! BFD_ASSEMBLER */
  801.  
  802. #ifndef WORKING_DOT_WORD
  803.   {
  804.     struct broken_word *lie;
  805.     struct broken_word **prevP;
  806.  
  807.     prevP = &broken_words;
  808.     for (lie = broken_words; lie; lie = lie->next_broken_word)
  809.       if (!lie->added)
  810.     {
  811. #ifdef BFD_ASSEMBLER
  812.       fix_new (lie->frag, lie->word_goes_here - lie->frag->fr_literal,
  813.            2, lie->add, lie->sub, lie->addnum, 0, BFD_RELOC_NONE);
  814. #else
  815. #if defined(TC_SPARC) || defined(TC_A29K) || defined(NEED_FX_R_TYPE)
  816.       fix_new (lie->frag, lie->word_goes_here - lie->frag->fr_literal,
  817.            2, lie->add,
  818.            lie->sub, lie->addnum,
  819.            0, NO_RELOC);
  820. #else
  821. #ifdef TC_NS32K
  822.       fix_new_ns32k (lie->frag,
  823.              lie->word_goes_here - lie->frag->fr_literal,
  824.              2,
  825.              lie->add,
  826.              lie->sub,
  827.              lie->addnum,
  828.              0, 0, 2, 0, 0);
  829. #else
  830.       fix_new (lie->frag, lie->word_goes_here - lie->frag->fr_literal,
  831.            2, lie->add,
  832.            lie->sub, lie->addnum,
  833.            0, 0);
  834. #endif /* TC_NS32K */
  835. #endif /* TC_SPARC|TC_A29K|NEED_FX_R_TYPE */
  836. #endif /* BFD_ASSEMBLER */
  837.       *prevP = lie->next_broken_word;
  838.     }
  839.       else
  840.     prevP = &(lie->next_broken_word);
  841.  
  842.     for (lie = broken_words; lie;)
  843.       {
  844.     struct broken_word *untruth;
  845.     char *table_ptr;
  846.     long table_addr;
  847.     long from_addr, to_addr;
  848.     int n, m;
  849.  
  850.     fragP = lie->dispfrag;
  851.  
  852.     /* Find out how many broken_words go here.  */
  853.     n = 0;
  854.     for (untruth = lie; untruth && untruth->dispfrag == fragP; untruth = untruth->next_broken_word)
  855.       if (untruth->added == 1)
  856.         n++;
  857.  
  858.     table_ptr = lie->dispfrag->fr_opcode;
  859.     table_addr = lie->dispfrag->fr_address + (table_ptr - lie->dispfrag->fr_literal);
  860.     /* Create the jump around the long jumps.  This is a short
  861.        jump from table_ptr+0 to table_ptr+n*long_jump_size.  */
  862.     from_addr = table_addr;
  863.     to_addr = table_addr + md_short_jump_size + n * md_long_jump_size;
  864.     md_create_short_jump (table_ptr, from_addr, to_addr, lie->dispfrag, lie->add);
  865.     table_ptr += md_short_jump_size;
  866.     table_addr += md_short_jump_size;
  867.  
  868.     for (m = 0; lie && lie->dispfrag == fragP; m++, lie = lie->next_broken_word)
  869.       {
  870.         if (lie->added == 2)
  871.           continue;
  872.         /* Patch the jump table */
  873.         /* This is the offset from ??? to table_ptr+0 */
  874.         to_addr = table_addr
  875.           - S_GET_VALUE (lie->sub);
  876.         md_number_to_chars (lie->word_goes_here, to_addr, 2);
  877.         for (untruth = lie->next_broken_word; untruth && untruth->dispfrag == fragP; untruth = untruth->next_broken_word)
  878.           {
  879.         if (untruth->use_jump == lie)
  880.           md_number_to_chars (untruth->word_goes_here, to_addr, 2);
  881.           }
  882.  
  883.         /* Install the long jump */
  884.         /* this is a long jump from table_ptr+0 to the final target */
  885.         from_addr = table_addr;
  886.         to_addr = S_GET_VALUE (lie->add) + lie->addnum;
  887.         md_create_long_jump (table_ptr, from_addr, to_addr, lie->dispfrag, lie->add);
  888.         table_ptr += md_long_jump_size;
  889.         table_addr += md_long_jump_size;
  890.       }
  891.       }
  892.   }
  893. #endif /* not WORKING_DOT_WORD */
  894.  
  895. #ifndef BFD_ASSEMBLER
  896. #ifndef    OBJ_VMS
  897.   {                /* not vms */
  898.     /*
  899.      * Scan every FixS performing fixups. We had to wait until now to do
  900.      * this because md_convert_frag() may have made some fixSs.
  901.      */
  902.     int trsize, drsize;
  903.  
  904.     subseg_change (SEG_TEXT, 0);
  905.     trsize = md_reloc_size * fixup_segment (text_fix_root,
  906.                         SEG_TEXT);
  907.     subseg_change (SEG_DATA, 0);
  908.     drsize = md_reloc_size * fixup_segment (data_fix_root,
  909.                         SEG_DATA);
  910.     H_SET_RELOCATION_SIZE (&headers, trsize, drsize);
  911.  
  912.     /* FIXME move this stuff into the pre-write-hook */
  913.     H_SET_MAGIC_NUMBER (&headers, magic_number_for_object_file);
  914.     H_SET_ENTRY_POINT (&headers, 0);
  915.  
  916.     obj_pre_write_hook (&headers);    /* extra coff stuff */
  917.  
  918.     object_file_size = H_GET_FILE_SIZE (&headers);
  919.     next_object_file_charP = the_object_file = xmalloc (object_file_size);
  920.  
  921.     output_file_create (out_file_name);
  922.  
  923.     obj_header_append (&next_object_file_charP, &headers);
  924.  
  925.     know ((next_object_file_charP - the_object_file) == H_GET_HEADER_SIZE (&headers));
  926.  
  927.     /*
  928.      * Emit code.
  929.      */
  930.     for (fragP = text_frag_root; fragP; fragP = fragP->fr_next)
  931.       {
  932.     register long count;
  933.     register char *fill_literal;
  934.     register long fill_size;
  935.  
  936.     know (fragP->fr_type == rs_fill);
  937.     append (&next_object_file_charP, fragP->fr_literal, (unsigned long) fragP->fr_fix);
  938.     fill_literal = fragP->fr_literal + fragP->fr_fix;
  939.     fill_size = fragP->fr_var;
  940.     know (fragP->fr_offset >= 0);
  941.  
  942.     for (count = fragP->fr_offset; count; count--)
  943.       {
  944.         append (&next_object_file_charP, fill_literal, (unsigned long) fill_size);
  945.       }            /* for each  */
  946.  
  947.       }                /* for each code frag. */
  948.  
  949.     know ((next_object_file_charP - the_object_file) == (H_GET_HEADER_SIZE (&headers) + H_GET_TEXT_SIZE (&headers) + H_GET_DATA_SIZE (&headers)));
  950.  
  951.     /*
  952.      * Emit relocations.
  953.      */
  954.     obj_emit_relocations (&next_object_file_charP, text_fix_root, (relax_addressT) 0);
  955.     know ((next_object_file_charP - the_object_file) == (H_GET_HEADER_SIZE (&headers) + H_GET_TEXT_SIZE (&headers) + H_GET_DATA_SIZE (&headers) + H_GET_TEXT_RELOCATION_SIZE (&headers)));
  956. #ifdef TC_I960
  957.     /* Make addresses in data relocation directives relative to beginning of
  958.      * first data fragment, not end of last text fragment:  alignment of the
  959.      * start of the data segment may place a gap between the segments.
  960.      */
  961.     obj_emit_relocations (&next_object_file_charP, data_fix_root, data0_frchainP->frch_root->fr_address);
  962. #else /* TC_I960 */
  963.     obj_emit_relocations (&next_object_file_charP, data_fix_root, text_last_frag->fr_address);
  964. #endif /* TC_I960 */
  965.  
  966.     know ((next_object_file_charP - the_object_file) == (H_GET_HEADER_SIZE (&headers) + H_GET_TEXT_SIZE (&headers) + H_GET_DATA_SIZE (&headers) + H_GET_TEXT_RELOCATION_SIZE (&headers) + H_GET_DATA_RELOCATION_SIZE (&headers)));
  967.  
  968.     /*
  969.      * Emit line number entries.
  970.      */
  971.     OBJ_EMIT_LINENO (&next_object_file_charP, lineno_rootP, the_object_file);
  972.     know ((next_object_file_charP - the_object_file) == (H_GET_HEADER_SIZE (&headers) + H_GET_TEXT_SIZE (&headers) + H_GET_DATA_SIZE (&headers) + H_GET_TEXT_RELOCATION_SIZE (&headers) + H_GET_DATA_RELOCATION_SIZE (&headers) + H_GET_LINENO_SIZE (&headers)));
  973.  
  974.     /*
  975.      * Emit symbols.
  976.      */
  977.     obj_emit_symbols (&next_object_file_charP, symbol_rootP);
  978.     know ((next_object_file_charP - the_object_file) == (H_GET_HEADER_SIZE (&headers) + H_GET_TEXT_SIZE (&headers) + H_GET_DATA_SIZE (&headers) + H_GET_TEXT_RELOCATION_SIZE (&headers) + H_GET_DATA_RELOCATION_SIZE (&headers) + H_GET_LINENO_SIZE (&headers) + H_GET_SYMBOL_TABLE_SIZE (&headers)));
  979.  
  980.     /*
  981.      * Emit strings.
  982.      */
  983.  
  984.     if (string_byte_count > 0)
  985.       {
  986.     obj_emit_strings (&next_object_file_charP);
  987.       }                /* only if we have a string table */
  988.  
  989. #ifdef BFD_HEADERS
  990.     bfd_seek (stdoutput, 0, 0);
  991.     bfd_write (the_object_file, 1, object_file_size, stdoutput);
  992. #else
  993.  
  994.     /* Write the data to the file */
  995.     output_file_append (the_object_file, object_file_size, out_file_name);
  996. #endif
  997.  
  998.     output_file_close (out_file_name);
  999.   }                /* non vms output */
  1000. #else /* VMS */
  1001.   /*
  1002.    *    Now do the VMS-dependent part of writing the object file
  1003.    */
  1004.   VMS_write_object_file (H_GET_TEXT_SIZE (&headers),
  1005.              H_GET_DATA_SIZE (&headers),
  1006.              H_GET_BSS_SIZE (&headers),
  1007.              text_frag_root, data_frag_root);
  1008. #endif /* VMS */
  1009. #else /* BFD_ASSEMBLER */
  1010.  
  1011. #ifdef obj_check_file_symbols
  1012.   obj_check_file_symbols ();
  1013. #endif
  1014.  
  1015.   /* Set up symbol table, and write it out.  */
  1016.   if (symbol_rootP)
  1017.     {
  1018.       int i = 0, n;
  1019.       symbolS *symp;
  1020.  
  1021.       for (symp = symbol_rootP; symp; symp = symbol_next (symp))
  1022.     {
  1023.       S_SET_VALUE (symp, S_GET_VALUE (symp) + symp->sy_frag->fr_address);
  1024.       /* So far, common symbols have been treated like undefined symbols.
  1025.          Put them in the common section now.  */
  1026.       if (S_IS_DEFINED (symp) == 0
  1027.           && S_GET_VALUE (symp) != 0)
  1028.         S_SET_SEGMENT (symp, &bfd_com_section);
  1029. #if 0
  1030.       printf ("symbol `%s'\n\t@%x: value=%d type=%d forward=%x seg=%s\n",
  1031.           S_GET_NAME (symp), symp,
  1032.           S_GET_VALUE (symp),
  1033.           S_GET_DATA_TYPE (symp),
  1034.           symp->sy_forward,
  1035.           segment_name (symp->bsym->section));
  1036. #endif
  1037.       {
  1038.         int punt = 0;
  1039. #ifdef obj_frob_symbol
  1040.         obj_frob_symbol (symp, punt);
  1041.         if (punt)
  1042.           goto punt_it;
  1043. #endif
  1044. #ifdef tc_frob_symbol
  1045.         tc_frob_symbol (symp, punt);
  1046.         if (punt)
  1047.           goto punt_it;
  1048. #endif
  1049.       }
  1050.       /* If we don't want to keep this symbol, splice it out of the
  1051.          chain now.  */
  1052.       if (S_IS_LOCAL (symp))
  1053.         {
  1054.           symbolS *prev, *next;
  1055.         punt_it:
  1056.           prev = symbol_previous (symp);
  1057.           next = symbol_next (symp);
  1058. #ifdef DEBUG
  1059.           verify_symbol_chain_2 (symp);
  1060. #endif
  1061.           if (prev)
  1062.         {
  1063.           symbol_next (prev) = next;
  1064.           symp = prev;
  1065.         }
  1066.           else if (symp == symbol_rootP)
  1067.         symbol_rootP = next;
  1068.           else
  1069.         abort ();
  1070.           if (next)
  1071.         symbol_previous (next) = prev;
  1072.           else
  1073.         symbol_lastP = prev;
  1074. #ifdef DEBUG
  1075.           if (prev)
  1076.         verify_symbol_chain_2 (prev);
  1077.           else if (next)
  1078.         verify_symbol_chain_2 (next);
  1079. #endif
  1080.           continue;
  1081.         }
  1082.       i++;
  1083.     }
  1084.       n = i;
  1085.       if (n)
  1086.     {
  1087.       asymbol **asympp;
  1088.       boolean result;
  1089.  
  1090.       asympp = (asymbol **) bfd_alloc (stdoutput, n * sizeof (asymbol *));
  1091.       symp = symbol_rootP;
  1092.       for (i = 0; i < n; i++, symp = symbol_next (symp))
  1093.         {
  1094.           asympp[i] = symp->bsym;
  1095.           symp->written = 1;
  1096.         }
  1097.       result = bfd_set_symtab (stdoutput, asympp, n);
  1098.       assert (result == true);
  1099.     }
  1100.     }
  1101.  
  1102. #ifdef obj_frob_file
  1103.   obj_frob_file ();
  1104. #endif
  1105.  
  1106.   /* Now that all the sizes are known, and contents correct, we can
  1107.      start writing the file.  */
  1108.   bfd_map_over_sections (stdoutput, write_contents, (char *) 0);
  1109.  
  1110.   output_file_close (out_file_name);
  1111. #endif /* BFD_ASSEMBLER */
  1112. }
  1113. #endif /* ! BFD */
  1114.  
  1115. /*
  1116.  *            relax_segment()
  1117.  *
  1118.  * Now we have a segment, not a crowd of sub-segments, we can make fr_address
  1119.  * values.
  1120.  *
  1121.  * Relax the frags.
  1122.  *
  1123.  * After this, all frags in this segment have addresses that are correct
  1124.  * within the segment. Since segments live in different file addresses,
  1125.  * these frag addresses may not be the same as final object-file addresses.
  1126.  */
  1127.  
  1128.  
  1129. static int 
  1130. is_dnrange (f1, f2)
  1131.      struct frag *f1;
  1132.      struct frag *f2;
  1133. {
  1134.   for (; f1; f1 = f1->fr_next)
  1135.     if (f1->fr_next == f2)
  1136.       return 1;
  1137.   return 0;
  1138. }
  1139.  
  1140. /* Relax_align. Advance location counter to next address that has 'alignment'
  1141.    lowest order bits all 0s.  */
  1142.  
  1143. /* How many addresses does the .align take? */
  1144. static relax_addressT
  1145. relax_align (address, alignment)
  1146.      register relax_addressT address;    /* Address now. */
  1147.      register long alignment;    /* Alignment (binary). */
  1148. {
  1149.   relax_addressT mask;
  1150.   relax_addressT new_address;
  1151.  
  1152.   mask = ~((~0) << alignment);
  1153.   new_address = (address + mask) & (~mask);
  1154.   if (linkrelax)
  1155.     /* We must provide lots of padding, so the linker can discard it
  1156.        when needed.  The linker will not add extra space, ever.  */
  1157.     new_address += (1 << alignment);
  1158.   return (new_address - address);
  1159. }
  1160.  
  1161. void 
  1162. relax_segment (segment_frag_root, segment)
  1163.      struct frag *segment_frag_root;
  1164.      segT segment;
  1165. {
  1166.   register struct frag *fragP;
  1167.   register relax_addressT address;
  1168. #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
  1169.   know (segment == SEG_DATA || segment == SEG_TEXT || segment == SEG_BSS);
  1170. #endif
  1171.   /* In case md_estimate_size_before_relax() wants to make fixSs. */
  1172.   subseg_change (segment, 0);
  1173.  
  1174.   /* For each frag in segment: count and store  (a 1st guess of)
  1175.      fr_address.  */
  1176.   address = 0;
  1177.   for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
  1178.     {
  1179.       fragP->fr_address = address;
  1180.       address += fragP->fr_fix;
  1181.  
  1182.       switch (fragP->fr_type)
  1183.     {
  1184.     case rs_fill:
  1185.       address += fragP->fr_offset * fragP->fr_var;
  1186.       break;
  1187.  
  1188.     case rs_align:
  1189.       address += relax_align (address, fragP->fr_offset);
  1190.       break;
  1191.  
  1192.     case rs_org:
  1193.       /* Assume .org is nugatory. It will grow with 1st relax.  */
  1194.       break;
  1195.  
  1196.     case rs_machine_dependent:
  1197.       address += md_estimate_size_before_relax (fragP, segment);
  1198.       break;
  1199.  
  1200. #ifndef WORKING_DOT_WORD
  1201.       /* Broken words don't concern us yet */
  1202.     case rs_broken_word:
  1203.       break;
  1204. #endif
  1205.  
  1206.     default:
  1207.       BAD_CASE (fragP->fr_type);
  1208.       break;
  1209.     }            /* switch(fr_type) */
  1210.     }                /* for each frag in the segment */
  1211.  
  1212.   /* Do relax().  */
  1213.   {
  1214.     long stretch;    /* May be any size, 0 or negative. */
  1215.     /* Cumulative number of addresses we have */
  1216.     /* relaxed this pass. */
  1217.     /* We may have relaxed more than one address. */
  1218.     long stretched;    /* Have we stretched on this pass? */
  1219.     /* This is 'cuz stretch may be zero, when, in fact some piece of code
  1220.        grew, and another shrank.  If a branch instruction doesn't fit anymore,
  1221.        we could be scrod.  */
  1222.  
  1223.     do
  1224.       {
  1225.     stretch = stretched = 0;
  1226.     for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
  1227.       {
  1228.         long growth = 0;
  1229.         unsigned long was_address;
  1230.         long offset;
  1231.         symbolS *symbolP;
  1232.         long target;
  1233.         long after;
  1234.         long aim;
  1235.  
  1236.         was_address = fragP->fr_address;
  1237.         address = fragP->fr_address += stretch;
  1238.         symbolP = fragP->fr_symbol;
  1239.         offset = fragP->fr_offset;
  1240.  
  1241.         switch (fragP->fr_type)
  1242.           {
  1243.           case rs_fill:    /* .fill never relaxes. */
  1244.         growth = 0;
  1245.         break;
  1246.  
  1247. #ifndef WORKING_DOT_WORD
  1248.         /* JF:  This is RMS's idea.  I do *NOT* want to be blamed
  1249.            for it I do not want to write it.  I do not want to have
  1250.            anything to do with it.  This is not the proper way to
  1251.            implement this misfeature.  */
  1252.           case rs_broken_word:
  1253.         {
  1254.           struct broken_word *lie;
  1255.           struct broken_word *untruth;
  1256.  
  1257.           /* Yes this is ugly (storing the broken_word pointer
  1258.              in the symbol slot).  Still, this whole chunk of
  1259.              code is ugly, and I don't feel like doing anything
  1260.              about it.  Think of it as stubbornness in action.  */
  1261.           growth = 0;
  1262.           for (lie = (struct broken_word *) (fragP->fr_symbol);
  1263.                lie && lie->dispfrag == fragP;
  1264.                lie = lie->next_broken_word)
  1265.             {
  1266.  
  1267.               if (lie->added)
  1268.             continue;
  1269.  
  1270.               offset = (lie->add->sy_frag->fr_address
  1271.                 + S_GET_VALUE (lie->add)
  1272.                 + lie->addnum
  1273.                 - (lie->sub->sy_frag->fr_address
  1274.                    + S_GET_VALUE (lie->sub)));
  1275.               if (offset <= -32768 || offset >= 32767)
  1276.             {
  1277.               if (flagseen['K'])
  1278.                 as_warn (".word %s-%s+%ld didn't fit",
  1279.                      S_GET_NAME (lie->add),
  1280.                      S_GET_NAME (lie->sub),
  1281.                      lie->addnum);
  1282.               lie->added = 1;
  1283.               if (fragP->fr_subtype == 0)
  1284.                 {
  1285.                   fragP->fr_subtype++;
  1286.                   growth += md_short_jump_size;
  1287.                 }
  1288.               for (untruth = lie->next_broken_word;
  1289.                    untruth && untruth->dispfrag == lie->dispfrag;
  1290.                    untruth = untruth->next_broken_word)
  1291.                 if ((untruth->add->sy_frag == lie->add->sy_frag)
  1292.                 && S_GET_VALUE (untruth->add) == S_GET_VALUE (lie->add))
  1293.                   {
  1294.                 untruth->added = 2;
  1295.                 untruth->use_jump = lie;
  1296.                   }
  1297.               growth += md_long_jump_size;
  1298.             }
  1299.             }
  1300.  
  1301.           break;
  1302.         }        /* case rs_broken_word */
  1303. #endif
  1304.           case rs_align:
  1305.         growth = (relax_align ((relax_addressT) (address
  1306.                              + fragP->fr_fix),
  1307.                        offset)
  1308.               - relax_align ((relax_addressT) (was_address
  1309.                                + fragP->fr_fix),
  1310.                      offset));
  1311.         break;
  1312.  
  1313.           case rs_org:
  1314.         target = offset;
  1315.  
  1316.         if (symbolP)
  1317.           {
  1318. #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
  1319.             know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
  1320.               || (S_GET_SEGMENT (symbolP) == SEG_DATA)
  1321.               || (S_GET_SEGMENT (symbolP) == SEG_TEXT)
  1322.               || S_GET_SEGMENT (symbolP) == SEG_BSS);
  1323.             know (symbolP->sy_frag);
  1324.             know (!(S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
  1325.               || (symbolP->sy_frag == &zero_address_frag));
  1326. #endif
  1327.             target += S_GET_VALUE (symbolP)
  1328.               + symbolP->sy_frag->fr_address;
  1329.           }        /* if we have a symbol */
  1330.  
  1331.         know (fragP->fr_next);
  1332.         after = fragP->fr_next->fr_address;
  1333.         growth = ((target - after) > 0) ? (target - after) : 0;
  1334.         /* Growth may be negative, but variable part of frag
  1335.            cannot have fewer than 0 chars.  That is, we can't
  1336.            .org backwards. */
  1337.  
  1338.         growth -= stretch;    /* This is an absolute growth factor */
  1339.         break;
  1340.  
  1341.           case rs_machine_dependent:
  1342.         {
  1343.           const relax_typeS *this_type;
  1344.           const relax_typeS *start_type;
  1345.           relax_substateT next_state;
  1346.           relax_substateT this_state;
  1347.  
  1348.           this_state = fragP->fr_subtype;
  1349.           start_type = this_type = md_relax_table + this_state;
  1350.           target = offset;
  1351.  
  1352.           if (symbolP)
  1353.             {
  1354. #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
  1355.               know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
  1356.                 || (S_GET_SEGMENT (symbolP) == SEG_DATA)
  1357.                 || (S_GET_SEGMENT (symbolP) == SEG_BSS)
  1358.                 || (S_GET_SEGMENT (symbolP) == SEG_TEXT));
  1359. #endif
  1360.               know (symbolP->sy_frag);
  1361.               know (!(S_GET_SEGMENT (symbolP) == absolute_section)
  1362.                 || symbolP->sy_frag == &zero_address_frag);
  1363.               target +=
  1364.             S_GET_VALUE (symbolP)
  1365.             + symbolP->sy_frag->fr_address;
  1366.  
  1367.               /* If frag has yet to be reached on this pass,
  1368.              assume it will move by STRETCH just as we did.
  1369.              If this is not so, it will be because some frag
  1370.              between grows, and that will force another pass.
  1371.  
  1372.              Beware zero-length frags.
  1373.  
  1374.              There should be a faster way to do this.  */
  1375.  
  1376.               if (symbolP->sy_frag->fr_address >= was_address
  1377.               && is_dnrange (fragP, symbolP->sy_frag))
  1378.             {
  1379.               target += stretch;
  1380.             }
  1381.             }
  1382.  
  1383.           aim = target - address - fragP->fr_fix;
  1384.           /* The displacement is affected by the instruction size
  1385.              for the 32k architecture. I think we ought to be able
  1386.              to add fragP->fr_pcrel_adjust in all cases (it should be
  1387.              zero if not used), but just in case it breaks something
  1388.              else we'll put this inside #ifdef NS32K ... #endif  */
  1389. #ifndef TC_NS32K
  1390.           if (fragP->fr_pcrel_adjust)
  1391.             abort ();
  1392. #endif
  1393.           aim += fragP->fr_pcrel_adjust;
  1394.  
  1395.           if (aim < 0)
  1396.             {
  1397.               /* Look backwards. */
  1398.               for (next_state = this_type->rlx_more; next_state;)
  1399.             if (aim >= this_type->rlx_backward)
  1400.               next_state = 0;
  1401.             else
  1402.               {
  1403.                 /* Grow to next state. */
  1404.                 this_state = next_state;
  1405.                 this_type = md_relax_table + this_state;
  1406.                 next_state = this_type->rlx_more;
  1407.               }
  1408.             }
  1409.           else
  1410.             {
  1411. #ifdef M68K_AIM_KLUDGE
  1412.               M68K_AIM_KLUDGE (aim, this_state, this_type);
  1413. #endif
  1414.               /* Look forwards. */
  1415.               for (next_state = this_type->rlx_more; next_state;)
  1416.             if (aim <= this_type->rlx_forward)
  1417.               next_state = 0;
  1418.             else
  1419.               {
  1420.                 /* Grow to next state. */
  1421.                 this_state = next_state;
  1422.                 this_type = md_relax_table + this_state;
  1423.                 next_state = this_type->rlx_more;
  1424.               }
  1425.             }
  1426.  
  1427.           growth = this_type->rlx_length - start_type->rlx_length;
  1428.           if (growth != 0)
  1429.             fragP->fr_subtype = this_state;
  1430.         }
  1431.         break;
  1432.  
  1433.           default:
  1434.         BAD_CASE (fragP->fr_type);
  1435.         break;
  1436.           }
  1437.         if (growth)
  1438.           {
  1439.         stretch += growth;
  1440.         stretched++;
  1441.           }
  1442.       }            /* For each frag in the segment. */
  1443.       }
  1444.     while (stretched);        /* Until nothing further to relax. */
  1445.   }                /* do_relax */
  1446.  
  1447.   /*
  1448.    * We now have valid fr_address'es for each frag.
  1449.    */
  1450.  
  1451.   /*
  1452.    * All fr_address's are correct, relative to their own segment.
  1453.    * We have made all the fixS we will ever make.
  1454.    */
  1455. }                /* relax_segment() */
  1456.  
  1457. /* fixup_segment()
  1458.  
  1459.    Go through all the fixS's in a segment and see which ones can be
  1460.    handled now.  (These consist of fixS where we have since discovered
  1461.    the value of a symbol, or the address of the frag involved.)
  1462.    For each one, call md_apply_fix to put the fix into the frag data.
  1463.  
  1464.    Result is a count of how many relocation structs will be needed to
  1465.    handle the remaining fixS's that we couldn't completely handle here.
  1466.    These will be output later by emit_relocations().  */
  1467.  
  1468. static long
  1469. fixup_segment (fixP, this_segment_type)
  1470.      register fixS *fixP;
  1471.      segT this_segment_type;    /* N_TYPE bits for segment. */
  1472. {
  1473.   register long seg_reloc_count;
  1474.   register symbolS *add_symbolP;
  1475.   register symbolS *sub_symbolP;
  1476.   long add_number;
  1477.   register int size;
  1478.   register char *place;
  1479.   register long where;
  1480.   register char pcrel;
  1481.   register fragS *fragP;
  1482.   register segT add_symbol_segment = absolute_section;
  1483.  
  1484.   seg_reloc_count = 0;
  1485.   /* If the linker is doing the relaxing, we must not do any fixups */
  1486.   if (linkrelax)
  1487.     for (; fixP; fixP = fixP->fx_next)
  1488.       seg_reloc_count++;
  1489.   else
  1490.     for (; fixP; fixP = fixP->fx_next)
  1491.       {
  1492.     fragP = fixP->fx_frag;
  1493.     know (fragP);
  1494.     where = fixP->fx_where;
  1495.     place = fragP->fr_literal + where;
  1496.     size = fixP->fx_size;
  1497.     add_symbolP = fixP->fx_addsy;
  1498. #ifdef TC_I960
  1499.     if (fixP->fx_callj && TC_S_IS_CALLNAME (add_symbolP))
  1500.       {
  1501.         /* Relocation should be done via the associated 'bal'
  1502.            entry point symbol. */
  1503.  
  1504.         if (!TC_S_IS_BALNAME (tc_get_bal_of_call (add_symbolP)))
  1505.           {
  1506.         as_bad ("No 'bal' entry point for leafproc %s",
  1507.             S_GET_NAME (add_symbolP));
  1508.         continue;
  1509.           }
  1510.         fixP->fx_addsy = add_symbolP = tc_get_bal_of_call (add_symbolP);
  1511.       }
  1512. #endif
  1513.     sub_symbolP = fixP->fx_subsy;
  1514.     add_number = fixP->fx_offset;
  1515.     pcrel = fixP->fx_pcrel;
  1516.  
  1517.     if (add_symbolP)
  1518.       add_symbol_segment = S_GET_SEGMENT (add_symbolP);
  1519.  
  1520.     if (sub_symbolP)
  1521.       {
  1522.         if (!add_symbolP)
  1523.           {
  1524.         /* Its just -sym */
  1525.         if (S_GET_SEGMENT (sub_symbolP) != absolute_section)
  1526.           as_bad ("Negative of non-absolute symbol %s",
  1527.               S_GET_NAME (sub_symbolP));
  1528.  
  1529.         add_number -= S_GET_VALUE (sub_symbolP);
  1530.           }
  1531.         else if ((S_GET_SEGMENT (sub_symbolP) == add_symbol_segment)
  1532.              && (SEG_NORMAL (add_symbol_segment)
  1533.              || (add_symbol_segment == absolute_section)))
  1534.           {
  1535.         /* Difference of 2 symbols from same segment.
  1536.            Can't make difference of 2 undefineds: 'value' means
  1537.            something different for N_UNDF. */
  1538. #ifdef TC_I960
  1539.         /* Makes no sense to use the difference of 2 arbitrary symbols
  1540.            as the target of a call instruction.  */
  1541.         if (fixP->fx_callj)
  1542.           {
  1543.             as_bad ("callj to difference of 2 symbols");
  1544.           }
  1545. #endif /* TC_I960 */
  1546.         add_number += S_GET_VALUE (add_symbolP) -
  1547.           S_GET_VALUE (sub_symbolP);
  1548.  
  1549.         add_symbolP = NULL;
  1550.         fixP->fx_addsy = NULL;
  1551.           }
  1552.         else
  1553.           {
  1554.         /* Different segments in subtraction. */
  1555.         know (!(S_IS_EXTERNAL (sub_symbolP)
  1556.             && (S_GET_SEGMENT (sub_symbolP) == absolute_section)));
  1557.  
  1558.         if ((S_GET_SEGMENT (sub_symbolP) == absolute_section))
  1559.           {
  1560.             add_number -= S_GET_VALUE (sub_symbolP);
  1561.           }
  1562.         else
  1563.           {
  1564.             as_bad ("Can't emit reloc {- %s-seg symbol \"%s\"} @ file address %d.",
  1565.                 segment_name (S_GET_SEGMENT (sub_symbolP)),
  1566.                S_GET_NAME (sub_symbolP), fragP->fr_address + where);
  1567.           }        /* if absolute */
  1568.           }
  1569.       }            /* if sub_symbolP */
  1570.  
  1571.     if (add_symbolP)
  1572.       {
  1573.         if (add_symbol_segment == this_segment_type && pcrel)
  1574.           {
  1575.         /*
  1576.          * This fixup was made when the symbol's segment was
  1577.          * SEG_UNKNOWN, but it is now in the local segment.
  1578.          * So we know how to do the address without relocation.
  1579.          */
  1580. #ifdef TC_I960
  1581.         /* reloc_callj() may replace a 'call' with a 'calls' or a
  1582.            'bal', in which cases it modifies *fixP as appropriate.
  1583.            In the case of a 'calls', no further work is required,
  1584.            and *fixP has been set up to make the rest of the code
  1585.            below a no-op. */
  1586.         reloc_callj (fixP);
  1587. #endif /* TC_I960 */
  1588.  
  1589.         add_number += S_GET_VALUE (add_symbolP);
  1590.         add_number -= md_pcrel_from (fixP);
  1591.         pcrel = 0;    /* Lie. Don't want further pcrel processing. */
  1592.         fixP->fx_addsy = NULL;    /* No relocations please. */
  1593.           }
  1594.         else
  1595.           {
  1596.         if (add_symbol_segment == absolute_section)
  1597.           {
  1598. #ifdef TC_I960
  1599.             /* See comment about reloc_callj() above.  */
  1600.             reloc_callj (fixP);
  1601. #endif /* TC_I960 */
  1602.             add_number += S_GET_VALUE (add_symbolP);
  1603.             fixP->fx_addsy = NULL;
  1604.             add_symbolP = NULL;
  1605.           }
  1606.         else if (add_symbol_segment == undefined_section
  1607. #ifdef BFD_ASSEMBLER
  1608.              || add_symbol_segment == &bfd_com_section
  1609. #endif
  1610.              )
  1611.           {
  1612. #ifdef TC_I960
  1613.             if ((int) fixP->fx_bit_fixP == 13)
  1614.               {
  1615.             /* This is a COBR instruction.  They have only a
  1616.              * 13-bit displacement and are only to be used
  1617.              * for local branches: flag as error, don't generate
  1618.              * relocation.
  1619.              */
  1620.             as_bad ("can't use COBR format with external label");
  1621.             fixP->fx_addsy = NULL;    /* No relocations please. */
  1622.             continue;
  1623.               }        /* COBR */
  1624. #endif /* TC_I960 */
  1625.  
  1626. #ifdef OBJ_COFF
  1627. #ifdef TE_I386AIX
  1628.             if (S_IS_COMMON (add_symbolP))
  1629.               add_number += S_GET_VALUE (add_symbolP);
  1630. #endif /* TE_I386AIX */
  1631. #endif /* OBJ_COFF */
  1632.             ++seg_reloc_count;
  1633.           }
  1634.         else
  1635.           {
  1636.             seg_reloc_count++;
  1637.             add_number += S_GET_VALUE (add_symbolP);
  1638.           }
  1639.           }            /* if not in local seg */
  1640.       }            /* if there was a + symbol */
  1641.  
  1642.     if (pcrel)
  1643.       {
  1644.         add_number -= md_pcrel_from (fixP);
  1645.         if (add_symbolP == 0)
  1646.           {
  1647.         fixP->fx_addsy = &abs_symbol;
  1648.         ++seg_reloc_count;
  1649.           }            /* if there's an add_symbol */
  1650.       }            /* if pcrel */
  1651.  
  1652.     if (!fixP->fx_bit_fixP)
  1653.       {
  1654.         if ((size == 1 &&
  1655.          (add_number & ~0xFF)
  1656.          && ((add_number & ~0xFF) != (-1 & ~0xFF)))
  1657.         || (size == 2
  1658.             && (add_number & ~0xFFFF)
  1659.             && ((add_number & ~0xFFFF) != (-1 & ~0xFFFF))))
  1660.           {
  1661.         as_bad ("Value of %d too large for field of %d bytes at 0x%x",
  1662.             add_number, size, fragP->fr_address + where);
  1663.           }            /* generic error checking */
  1664. #ifdef WARN_SIGNED_OVERFLOW_WORD
  1665.         /* Warn if a .word value is too large when treated as a signed
  1666.            number.  We already know it is not too negative.  This is to
  1667.            catch over-large switches generated by gcc on the 68k.  */
  1668.         if (!flagseen['J']
  1669.         && size == 2
  1670.         && add_number > 0x7fff)
  1671.           as_bad ("Signed .word overflow; switch may be too large; %d at 0x%x",
  1672.               add_number, fragP->fr_address + where);
  1673. #endif
  1674.       }            /* not a bit fix */
  1675.  
  1676. #ifdef BFD_ASSEMBLER
  1677.     md_apply_fix (fixP, &add_number);
  1678. #else
  1679.     md_apply_fix (fixP, add_number);
  1680. #endif
  1681.       }                /* For each fixS in this segment. */
  1682.  
  1683. #ifdef OBJ_COFF
  1684. #ifdef TC_I960
  1685.   {
  1686.     fixS *topP = fixP;
  1687.  
  1688.     /* two relocs per callj under coff. */
  1689.     for (fixP = topP; fixP; fixP = fixP->fx_next)
  1690.       {
  1691.     if (fixP->fx_callj && fixP->fx_addsy != 0)
  1692.       {
  1693.         ++seg_reloc_count;
  1694.       }            /* if callj and not already fixed. */
  1695.       }                /* for each fix */
  1696.   }
  1697. #endif /* TC_I960 */
  1698.  
  1699. #endif /* OBJ_COFF */
  1700.   return (seg_reloc_count);
  1701. }                /* fixup_segment() */
  1702.  
  1703. /* end of write.c */
  1704.